08. Euler Angles from a Rotation Matrix

Euler Angles from a Rotation Matrix

In many cases, particularly involving inverse kinematics, we are given a composite rotation matrix and it is necessary to find a set of Euler Angles that would produce this rotation. The specific solution depends on the choice of Euler Angles but the basic procedure is the same. We illustrate the general procedure with an example.

Consider the extrinsic (i.e., fixed axis) X-Y-Z rotation sequence. The composite rotation matrix is,

\mathbf{_{B}^{A}\textrm{R}_{XYZ}= R_{Z}(\alpha)R_{Y}(\beta)R_{X}(\gamma) }
\mathbf{_{B}^{A}\textrm{R}_{XYZ}= \begin{bmatrix} r_{11} & r_{12} & r_{13}\\ r_{21} & r_{22} & r_{23}\\ r_{31}& r_{32} & r_{33} \end{bmatrix} = \begin{bmatrix} c_{\alpha}c_{\beta} & c_{\alpha}s_{\beta}s_{\gamma}-s_{\alpha}c_{\gamma} & c_{\alpha}s_{\beta}c_{\gamma}+s_{\alpha}s_{\gamma} \\ s_{\alpha}c_{\beta} & s_{\alpha}s_{\beta}s_{\gamma}+c_{\alpha}c_{\gamma} & s_{\alpha}s_{\beta}c_{\gamma}-c_{\alpha}s_{\gamma} \\ -s_{\beta}& c_{\beta}s_{\gamma}& c_{\beta}c_{\gamma} \end{bmatrix} }

The goal is to find the angles alpha, beta, and gamma, given that numerical values for r_{ij} are known. The solution is to use various combinations of r_{ij} so that each angle can be individually isolated and solved explicitly. Using the simplest terms possible seems to be a prudent strategy. Although beta appears in isolation in element r_{31} , it is not a good idea to solve for angles using the inverse of the sine or cosine functions. The reason is the ambiguity in sign: if -sin(beta) = 0.5, in which quadrant is the angle? This type of ambiguity is avoided by using the atan2 function.The syntax depends on the language or library used, but is often: atan2(y, x).

Thus, it is possible to find beta, by recognizing,

\mathbf{\beta = atan2(y,x) = atan2(-r_{31},\sqrt{r_{11}*r_{11} + r_{21}*r_{21}} )}

A similar trick is used to find gamma,

\mathbf{\gamma = atan2(r_{32},r_{33}) }

and also alpha,

\mathbf{\alpha = atan2(r_{21},r_{11}) }

What happens then when cos(beta) = 0, that is, when beta = +/-90 degrees? At this point, atan2 is undefined and, as we saw with Euler Angles, the system exhibits a singularity of representation.

Start Quiz:

#!/usr/bin/env python
import numpy as np
from sympy.matrices import Matrix
from sympy import symbols, atan2, sqrt


# Fixed Axis X-Y-Z Rotation Matrix
R_XYZ = Matrix([[ 0.353553390593274, -0.306186217847897, 0.883883476483184],
            [ 0.353553390593274,  0.918558653543692, 0.176776695296637],
            [-0.866025403784439,               0.25, 0.433012701892219]])

######## TO DO ##########
# Calculate the Euler angles that produces a rotation equivalent to R (above)
# NOTE: Be sure your answer has units of DEGREES!
alpha = 1 # rotation about Z-axis
beta  = 1 # rotation about Y-axis
gamma = 1 # rotation about X-axis

#!/usr/bin/env python
import numpy as np
from sympy.matrices import Matrix
from sympy import symbols, atan2, sqrt

# Conversion Factors
rtd = 180/np.pi
dtr = np.pi/180

# Fixed Axis X-Y-Z Rotation Matrix
R_XYZ = Matrix([[ 0.353553390593274, -0.306186217847897, 0.883883476483184],
                [ 0.353553390593274,  0.918558653543692, 0.176776695296637],
                [-0.866025403784439,               0.25, 0.433012701892219]])

### Identify useful terms from rotation matrix
r31 = R_XYZ[2,0]
r11 = R_XYZ[0,0]
r21 = R_XYZ[1,0]
r32 = R_XYZ[2,1]
r33 = R_XYZ[2,2]


### Euler Angles from Rotation Matrix
  # sympy synatx for atan2 is atan2(y, x)
beta  = atan2(-r31, sqrt(r11 * r11 + r21 * r21)) * rtd
gamma = atan2(r32, r33) * rtd
alpha = atan2(r21, r11) * rtd


print("alpha is = ",alpha*dtr, "radians", "or ", alpha, "degrees")
print("beta  is = ",beta*dtr,  "radians", "or ", beta, "degrees")
print("gamma is = ",gamma*dtr, "radians", "or ", gamma, "degrees")